package com.service.impl;

import com.common.exception.db.InsertException;
import com.dao.BillDao;
import com.dao.LoginDao;
import com.entity.LoginInfo;
import com.entity.User;
import com.service.LoginService;
import com.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * Created by Administrator on 2018/10/20 0020.
 */
@Service
public class LoginServiceImpl implements LoginService{
    @Autowired
    private LoginDao loginDao;
    @Autowired
    private BillDao billDao;

    @Override
    public LoginInfo findPhone(String phone, String password,Integer type) {
        String typeName;
        if(type == 1){
            typeName = "student";
            return loginDao.findPhone(phone,password,typeName);
        }else{
            typeName = "teacher";
            LoginInfo loginInfo = loginDao.findPhone(phone,password,typeName);
            int find = loginDao.findTeacherIsBeing(loginInfo.getId());
            if(find > 0){
                return loginInfo;
            }else{
                find = loginDao.addTeacher(loginInfo.getId());
                if(find > 0){
                    return loginInfo;
                }else{
                    return loginInfo;
                }
            }
        }
    }

    @Override
    public String getOpenId(Integer userId) {
        return loginDao.getOpenId(userId);
    }

    @Override
    public int insertCertification(Integer userId) {
        int find = loginDao.findIsCard(userId);
        if(find > 0 ){
            return 0;
        }else{
            return loginDao.insertCertification(userId);
        }

    }

    @Override
    @Transactional
    public int registration(String phone,String password,Integer type,Integer pid) {
        String typeName;
        int success = loginDao.findPhoneIsBeing(phone);
        if(success > 0){
            return -1;
        }else{
            if(type == 1){
                typeName = "student";
            }else{
                typeName = "teacher";
            }
            User user = new User();
            user.setTypeName(typeName);
            user.setPhone(phone);
            user.setPassword(password);
            success = loginDao.insertUser(user);
            if(success > 0){
                if(typeName.equals("student")){
                   success = loginDao.addFreeMoney(user.getId());
                   if(success > 0){
                       String uuid = UUIDUtil.get32UUID();
                       BigDecimal a = new BigDecimal(100);
                      success = billDao.insertBill(user.getId(),uuid,a,1,"系统赠送");
                      if(success > 0){
                          return success;
                      }else{
                          throw new InsertException("新增流水失败");
                      }
                   }else{
                       throw new InsertException("新增免费领英币失败 ");
                   }
                }
                if(pid != 0){
                    success = loginDao.insertUserP(user.getId(),pid);
                    if(success > 0){
                        return success;
                    }else{
                        throw new InsertException("新增失败");
                    }
                }else{
                    return success;
                }
            }else{
                throw new InsertException("新增失败");
            }
        }
    }

    @Override
    public int addTeacher(Integer userId) {
        return loginDao.addTeacher(userId);
    }

    @Override
    public int updatePassword(String phone, String password) {
        int success = loginDao.findPhoneIsBeing(phone);
        if(success > 0){
            success = loginDao.updatePassword(phone,password);
            if(success > 0){
                return success;
            }else{
                return 0;
            }
        }else{
            return 0;
        }

    }

    @Override
    public int reSetPassword(Integer userId, String password, String newPassword) {
        int success = loginDao.findPasswordIsCorrect(userId,password);
        if(success > 0){
            success = loginDao.updatePasswords(userId,newPassword);
            if(success > 0){
                return success;
            }else{
                return 0;
            }
        }else{
            return 0;
        }
    }

    @Override
    public int setPhone(String phone,String newPhone) {
        int success = loginDao.findPhoneIsBeing(phone);
        if(success>0){
            success = loginDao.findPhoneIsBeing(newPhone);
            if(success > 0){
                return -1;
            }else{
               success = loginDao.setPhone(phone,newPhone);
               if(success > 0){
                   return success;
               }else{
                   return 0;
               }
            }
        }else{
            return 0;
        }
    }

    @Override
    public int insertLoginLocation(Integer userId, String latitude, String longitude, String address) {
        int count = loginDao.findLoginLcationCount(userId,address);
        if(count > 0){
            return 1;
        }else{
            count = loginDao.insertLoginLocation(userId,latitude,longitude,address);
            if(count > 0){
                return 1;
            }else{
                return 0;
            }
        }
    }

    @Override
    public int findTimedTask() {
        return loginDao.findTimedTask();
    }
}
